﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Member.Client.Net
{
    public class Brush
    {
        private static int rep = 0;
        private static Random Rd = new Random();  //'实例化1个RANDOM



        public static void Get(string down,string apk_md5,string id,List<string> varsion)
        {
            //down=http%3A%2F%2Fm.shouji.360tpcdn.com%2F360sj%2Fdev%2F20130809%2Fcom.yqq.edu_4_164550.apk
            //apk_md5=5c6c89e7cf66d19add1e7eb68459d8a3;
            int x = varsion.Count / 10;
            x = varsion.Count / x;
            string msg = "";
            msg = Rd.Next(0, x).ToString();

            string url = "http://res.qhsetup.com/360baohe/download.php?resurl=" + down + "&m=" +
md5(GenerateCheckCode(14)) + "&m2=" + apk_md5 + "&fm=home_5_6_3&v=" + varsion[int.Parse(msg)] + "&re=1&ch=600000&si="+id;
            NetCommon.GetCode(url);
        }


        /// <summary>
        /// 获取随机数.
        /// </summary>
        /// <param name="codeCount"></param>
        /// <returns></returns>
        public static string GenerateCheckCode(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }
        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string md5(string key)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").ToLower();
        }

        public static List<string> GetVarsion()
        {
            List<string> list=new List<string>();
            string code = NetCommon.GetCode("http://www.360.cn/shoujizhushou/update.html");
            string reg=@"<h2 class=""tit_3"">360手机助手\(安卓版\)V(?<varsion>.+?)版本";
            MatchCollection mc = Regex.Matches(code, reg, RegexOptions.Singleline);
            foreach (Match match in mc)
            {
                if (match.Success)
                {
                    list.Add(match.Groups["varsion"].Value);
                }
            }
            return list;
        }


    }
}
